package com.wjjm.spoonselling.controller;

import com.wjjm.spoonselling.pojo.Comment;
import com.wjjm.spoonselling.service.CommentService;
import com.wjjm.spoonselling.util.Code;
import com.wjjm.spoonselling.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/selectAll/{commodityId}")
    public Result selectAll(@PathVariable int commodityId){
        List<Comment> comments = commentService.selectAll(commodityId);
        int code  = comments != null ? Code.SELECT_OK:Code.SELECT_ERR;
        String msg = comments != null ? "数据查询成功":"数据查询失败，请重试";
        return new Result(code,comments,msg);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Comment comment){
        Boolean i = commentService.insert(comment);
        int code  = i == true ? Code.ADD_OK : Code.ADD_ERR;
        String msg = i == true ? "留言添加成功":"留言添加失败，请重试";
        return new Result(code,i,msg);
    }
}
